package com.noble.meether.chat.ui.system;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.noble.glamour.module.logic.baseui.ExBaseAdapter;
import com.noble.glamour.module.util.UIShow;
import com.noble.library.image.ImageLoader;
import com.noble.meether.R;
import com.noble.meether.chat.ui.system.bean.SysNotice;

import java.util.List;

/**
 * 系统消息
 */
public class SysMessAdapter extends ExBaseAdapter<SysNotice> {
    private Context mContext;

    public SysMessAdapter(Context mContext, List<SysNotice> datas) {
        super(mContext, datas);
        this.mContext = mContext;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final MyViewHolder mHolder;
        if (convertView == null) {
            convertView = inflate(R.layout.sys_mess_item);
            mHolder = new MyViewHolder(convertView);
            convertView.setTag(mHolder);
        } else {
            mHolder = (MyViewHolder) convertView.getTag();
        }
        final SysNotice info = getItem(position);
        mHolder.tvTips.setText(info.getTimeCreate());
        mHolder.tvTitle.setText(info.getTitle());
        mHolder.tvContent.setText(info.getText());

        // 图片
        if (!TextUtils.isEmpty(info.getImage())) {
            mHolder.imgPic.setVisibility(View.VISIBLE);
            ImageLoader.loadCenterCrop(mContext, info.getImage(), mHolder.imgPic, 0, 0);
        } else {
            mHolder.imgPic.setVisibility(View.GONE);
        }

        // 跳转
        if (!TextUtils.isEmpty(info.getUrl())) {
            mHolder.ll_jump.setVisibility(View.VISIBLE);
            mHolder.tvJump.setOnClickListener(view -> UIShow.showWebActivity(mContext, info.getUrl()));
        } else {
            mHolder.ll_jump.setVisibility(View.GONE);
        }
        return convertView;
    }

    class MyViewHolder {
        TextView tvTips, tvTitle, tvContent, tvJump;
        ImageView imgPic;
        LinearLayout ll_jump;

        public MyViewHolder(View convertView) {
            initView(convertView);
        }

        private void initView(View convertView) {
            ll_jump = convertView.findViewById(R.id.ll_jump);
            tvTips = convertView.findViewById(R.id.sys_mess_item_tv_tips);
            tvTitle = convertView.findViewById(R.id.sys_mess_item_tv_title);
            tvContent = convertView.findViewById(R.id.sys_mess_item_tv_content);
            tvJump = convertView.findViewById(R.id.sys_mess_item_tv_jump);
            imgPic = convertView.findViewById(R.id.sys_mess_item_img_pic);
        }
    }
}